<?php
	require_once ("config.php");
	
	function dbconnect() 
	{ 
		global $dbhost, $dbuser, $dbpassword, $dbdatabase;
		static $connect = null; 
		
		if ($connect === null){ 
			$connect = mysql_connect($dbhost, $dbuser, $dbpassword); 
			mysql_select_db($dbdatabase, $connect); 
			mysql_query("SET NAMES 'utf8'", $connect);
		} 
		return $connect; 
	} 
	

//************* Data San Pham START **************	
	function GetSpHomepage($limit){
		return GetSp();
	}
	
	function GetNumSp(){
		$sql = mysql_query("SELECT count(id) as num FROM `sp` ", dbconnect());
		$result = mysql_fetch_assoc($sql);
		return $result['num'];
	}
	function GetSp()
	{
		$sql = mysql_query("SELECT * FROM `sp` ORDER BY date DESC", dbconnect());
		
		$result = array(); 
		while ($spRow = mysql_fetch_assoc($sql)) { 
			$result[] = $spRow; 
		} 
		return $result; 
	}
	function GetSp2($curPage, $rowPerPage)
	{
		if($curPage > 0)
			$curPage--;
		$from = $curPage * $rowPerPage;
				
		$sql = "SELECT * FROM `sp` ORDER BY `date` DESC LIMIT $from , $rowPerPage";
		$sql = mysql_query($sql);
		
		$result = array(); 
		while ($spRow = mysql_fetch_assoc($sql)) { 
			$result[] = $spRow; 
		} 
		return $result; 
	}
	
	function GetSpByLoai($id_loai){
		$sql = mysql_query("SELECT * FROM `sp` WHERE sp.`id_loai` = $id_loai", dbconnect());
		
		$result = array(); 
		while ($spRow = mysql_fetch_assoc($sql)) { 
			$result[] = $spRow; 
		} 
		return $result; 
	}

	function GetSpById($id_sp){
		$sql = mysql_query("SELECT * FROM `sp` WHERE sp.`id` = $id_sp", dbconnect());
		
		return mysql_fetch_assoc($sql);
	}
	
	function GetSpFromCart($cart){
		$result = array(); 
		$temp = array();
		
		foreach($cart as $value)
		{
			$id = key($cart);
			$sql = mysql_query("SELECT * FROM `sp` WHERE sp.`id` = $id", dbconnect());
			$temp =  mysql_fetch_assoc($sql);
			
			$temp['sl'] = $cart[$id];
			$temp['total'] = $temp['gia'] * $temp['sl'];
			$result[] = $temp;
			next($cart);
			
		}
		return $result;
	}
	function InsertSp($arr, $img_url)
	{
		if(!isset($arr['gia']) || empty($arr['gia']))
			$arr['gia'] = 0;
			
		$sql = "INSERT INTO sp (`ten_sp`, `id_loai`, `id_nhan_hieu`, `gia`, `mo_ta_ngan`, `mo_ta_dai`, `date`, `img_url`) 
				VALUE ('{$arr['ten_sp']}', {$arr['id_loai']}, {$arr['id_nhan_hieu']}, {$arr['gia']}, '{$arr['mo_ta_ngan']}', '{$arr['mo_ta_dai']}', NOW(),'{$img_url}'   )";

		mysql_query($sql, dbconnect());
	}
	
	function DeleteSp($id){
		$sql = mysql_query("DELETE FROM `sp` WHERE `id`= $id", dbconnect());
	}
	
	function EditSp($id_sp, $arr, $img_url)
	{
		if(!isset($arr['gia']) || empty($arr['gia']))
			$arr['gia'] = 0;
			
		// var_dump($arr); die();
		if ($img_url)
			$sql = "UPDATE sp 
							SET `ten_sp`='{$arr['ten_sp']}', `id_loai`={$arr['id_loai']}, `id_nhan_hieu` = {$arr['id_nhan_hieu']},
								`gia` = {$arr['gia']}, `mo_ta_ngan` = '{$arr['mo_ta_ngan']}',
								`mo_ta_dai` = '{$arr['mo_ta_dai']}', `date` = NOW(),
								`img_url` = '{$img_url}' 
							WHERE `id`=$id_sp";
		else
			$sql = "UPDATE sp 
							SET `ten_sp`='{$arr['ten_sp']}', `id_loai`={$arr['id_loai']}, `id_nhan_hieu` = {$arr['id_nhan_hieu']},
								`gia` = {$arr['gia']}, `mo_ta_ngan` = '{$arr['mo_ta_ngan']}',
								`mo_ta_dai` = '{$arr['mo_ta_dai']}', `date` = NOW()
							WHERE `id`=$id_sp";
		
		// echo $sql; die();
		mysql_query($sql, dbconnect());
	}
//************* Data San Pham END **************	
	
	
	
//******** Data Nhom START *********	
	function GetNhom() 
	{ 
		$sql = mysql_query("SELECT * FROM nhom_sp", dbconnect()); 
		
		$result = array(); 
		while ($nhomRow = mysql_fetch_assoc($sql)) { 
			$result[] = $nhomRow;
		} 
		return $result; 
	} 
	
	function InsertNhom($ten_nhom)
	{
		mysql_query("	INSERT INTO nhom_sp (`ten_nhom`) 
						VALUE ('$ten_nhom')", dbconnect());
	}
	
	function GetNhomById($id_nhom){
		$sql = mysql_query("SELECT * FROM `nhom_sp` WHERE nhom_sp.`id_nhom` = $id_nhom", dbconnect());
		
		return mysql_fetch_assoc($sql);
	}
	
	function EditNhom($id_nhom, $ten_nhom)
	{
		$sql = mysql_query("	UPDATE nhom_sp 
								SET `ten_nhom`='$ten_nhom'
								WHERE `id_nhom`=$id_nhom", dbconnect());
			
	}
	
	function DeleteNhom($id_nhom){
		$sql = mysql_query("DELETE FROM `nhom_sp` WHERE `id_nhom`= $id_nhom", dbconnect());
	}
	
//******* Data Nhom END ****************
	

//***** Data Loai START *********	
	function GetLoaiName($id_loai){
		$sql = mysql_query("SELECT * FROM `loai_sp` WHERE loai_sp.`id_loai` = $id_loai", dbconnect());
		
		$row =  mysql_fetch_assoc($sql);
		if ($row)
			return  $row['ten_loai'];
		else
			return null;
	}
	
	function GetLoai(){
		$sql = mysql_query("SELECT * FROM `loai_sp`", dbconnect());
		
		$result = array(); 
		while ($loaiRow = mysql_fetch_assoc($sql)) { 
			$result[] = $loaiRow; 
		} 
		return $result; 
	}
	
	function GetLoaiById($id_loai){
		$sql = mysql_query("SELECT * FROM `loai_sp` WHERE loai_sp.`id_loai` = $id_loai", dbconnect());
		
		return mysql_fetch_assoc($sql);
	}
	
	function InsertLoai($ten_loai, $id_nhom)
	{
		$sql = "INSERT INTO loai_sp (`ten_loai`, `id_nhom`) VALUE ('$ten_loai', $id_nhom)";
	
		mysql_query($sql, dbconnect());
	}
	
	function EditLoai($id_loai, $arr)
	{
		$sql = "	UPDATE loai_sp 
					SET `ten_loai`='{$arr['ten_loai']}', `id_nhom`={$arr['id_nhom']}
					WHERE `id_loai`=$id_loai";
		// echo $sql; die();
		mysql_query($sql, dbconnect());
			
	}
	
	function DeleteLoai($id_loai){
		$sql = mysql_query("DELETE FROM `loai_sp` WHERE `id_loai`= $id_loai", dbconnect());
	}
//***** Data Loai END *********

//******** Data Hieu START *********	
	function GetHieu() 
	{ 
		$sql = mysql_query("SELECT * FROM nhan_hieu", dbconnect()); 
		
		$result = array(); 
		while ($hieuRow = mysql_fetch_assoc($sql)) { 
			$result[] = $hieuRow;
		} 
		return $result; 
	} 
	
	function InsertHieu($ten_nhan_hieu)
	{
		mysql_query("	INSERT INTO nhan_hieu (`ten_nhan_hieu`) 
						VALUE ('$ten_nhan_hieu')", dbconnect());
	}
	
	function GetHieuById($id_nhan_hieu){
		$sql = mysql_query("SELECT * FROM `nhan_hieu` WHERE nhan_hieu.`id_nhan_hieu` = $id_nhan_hieu", dbconnect());
		
		return mysql_fetch_assoc($sql);
	}
	
	function EditHieu($id_nhan_hieu, $ten_nhan_hieu)
	{
		$sql = mysql_query("	UPDATE nhan_hieu 
								SET `ten_nhan_hieu`='$ten_nhan_hieu'
								WHERE `id_nhan_hieu`=$id_nhan_hieu", dbconnect());
			
	}
	
	function DeleteHieu($id_nhan_hieu){
		$sql = mysql_query("DELETE FROM `nhan_hieu` WHERE `id_nhan_hieu`= $id_nhan_hieu", dbconnect());
	}
//************** Data Hieu END *************************

	
?>